package oop.analyzer.method;

import oop.analyzer.AnalyzerMethod;
import oop.data.DataStore;
import oop.data.RowData;

import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FilterMethod implements AnalyzerMethod {
    @Override
    public DataStore execute(DataStore dataStore) {
        Iterator<RowData> iterator = dataStore.getIterator();
        while (iterator.hasNext()) {
            RowData rowData = iterator.next();
            if (isBad(rowData)) {
                iterator.remove();
            }
        }
        return dataStore;
    }
    
    
    public static final Pattern POSITIVE_FLOAT_PATTERN = Pattern.compile("^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*$");
    /**
     * 过滤的列,TODO 去掉了 Volume 因为这类都不是浮点数
     */
    public static final String[] VALIDATE_COLUMN = new String[]{"Open", "High", "Low", "Close", "Adj Close"};
    
    /**
     * 根据条件分析是必须为正浮点数

     */
    private boolean isBad(RowData rowData) {
        
        for (String columnName : VALIDATE_COLUMN) {
            String value = rowData.getValue(columnName);
            Matcher matcher = POSITIVE_FLOAT_PATTERN.matcher(value);
            if (!matcher.matches()) {
                return true;
            }
        }
        
        return false;
    }
  
}
